﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistente.Administrador
{
    public class PAdministrador
    {
        private DB_SIMETRIA _db;
        private SqlConnection _conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_SIMETRIA_SQL"].ConnectionString);
        public bool Salvar(string nmAdministrador, string teEmail, string teSenha, int csStatus, int csTipoUsuario)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var tbAdministrador = new tb_usuario_administracao();
                tbAdministrador.nm_usuario_administracao = nmAdministrador;
                tbAdministrador.te_email = teEmail;
                tbAdministrador.te_senha = teSenha;
                tbAdministrador.cs_tipo_usuario = csTipoUsuario;
                tbAdministrador.cs_status = csStatus;
                _db.AddTotb_usuario_administracao(tbAdministrador);
                _db.SaveChanges();

                return true;

            }catch(Exception e)
            {
                return false;
            }finally
            {
                _db.Connection.Close();
            }

        }

        public tb_usuario_administracao verificarLogin(string teEmail, string teSenha)
        {
            
            var obUsuarioAdministracao = new tb_usuario_administracao();
            try{
                _db = new DB_SIMETRIA();
                obUsuarioAdministracao = _db.tb_usuario_administracao.Where(q => q.te_email == teEmail && q.te_senha == teSenha && q.cs_status != 3).FirstOrDefault();
            }catch(Exception e){
                throw;
            }finally{
                _db.Connection.Close();
            }
            return obUsuarioAdministracao;

        }

        public DataSet pegarAdministradores(string nmAdministrador = null, string teEmail = null, int csStatus = 0, int csTipoUsuario = 0)
        {

            var ds = new DataSet();

            string query = "SELECT ";
            query += " nm_usuario_administracao";
            query += ", id_usuario_administracao";
            query += ", te_email";
            query += ", cs_status";
            query += ", cs_tipo_usuario";
            query += " FROM tb_usuario_administracao";

            query += " WHERE 1 = 1";

            if(csStatus != 0)
            {
                query += " AND cs_status =" + csStatus;
            }
            if(csTipoUsuario != 0)
            {
                query += "AND cs_tipo_usuario = " + csTipoUsuario;
            }
            if (!string.IsNullOrEmpty(nmAdministrador))
            {
                query += " AND nm_usuario_administracao LIKE '%" + nmAdministrador + "%'";
            }if (!string.IsNullOrEmpty(teEmail))
            {
                query += " AND te_email LIKE '%" + teEmail + "%'";
            }

            var cmd = new SqlCommand(query, _conexao);

            cmd.Connection = _conexao;

            if (_conexao.State == ConnectionState.Closed)
            {
                _conexao.Open();
            }

            var dataAdapter = new SqlDataAdapter(cmd);
            dataAdapter.Fill(ds);

            _conexao.Close();

            return ds;
        }

        public bool Remover(int idAdministrador)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbAdmin = _db.tb_usuario_administracao.Where(q => q.id_usuario_administracao == idAdministrador).FirstOrDefault();
                if(obTbAdmin == null)
                {
                    return false;
                }else
                {
                    obTbAdmin.cs_status = 3;
                    _db.SaveChanges();
                    return true;
                }

            }catch(Exception e)
            {
                throw;
            }finally
            {
                _db.Connection.Close();
            }
        }

        public tb_usuario_administracao pegarAdministradorPorId(int idAdministrador)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_usuario_administracao.Where(q => q.id_usuario_administracao == idAdministrador).FirstOrDefault();
            }catch(Exception e)
            {
                throw;
            }finally
            {
                _db.Connection.Close();
            }
        }

        public bool Alterar(int idAdministrador, tb_usuario_administracao obTbAdminNovo)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbAdminVelho =
                    _db.tb_usuario_administracao.Where(q => q.id_usuario_administracao == idAdministrador).FirstOrDefault();

                obTbAdminVelho.nm_usuario_administracao = obTbAdminNovo.nm_usuario_administracao;
                obTbAdminVelho.te_email = obTbAdminNovo.te_email;
                obTbAdminVelho.cs_status = obTbAdminNovo.cs_status;
                obTbAdminVelho.cs_tipo_usuario = obTbAdminNovo.cs_tipo_usuario;

                _db.SaveChanges();
                return true;
            }catch(Exception e)
            {
                return false;
            }finally
            {
                _db.Connection.Close();
            }
        }
    }
}
